Create Temporary File
Description
The create_temp_file
function is responsible for creating a temporary file on the web service that will be deleted from S3 after 24 hours. It accepts parameters like the device name, file name, file size, and SHA256 hash. This function returns information such as the response code, response string, signed URL for uploading the file, and the file ID.
Function Signature:
def create_temp_file(ws_config: actionstreamer.Config.WebServiceConfig, device_name: str, filename: str, file_size: int, sha256_hash: str) -> tuple[int, str, str, int]:
Parameters
- ws_config (WebServiceConfig): Configuration object for the web service.
- device_name (str): The device serial number associated with the file.
- filename (str): The name of the file (without path).
- file_size (int): The size of the file in bytes.
- sha256_hash (str): The SHA256 hash of the file to ensure integrity.
Returns
- tuple[int, str, str, int]: A tuple containing:
- response_code: The HTTP response code from the server (e.g., 200 for success).
- response_string: The response body from the server, which may contain error messages or additional details.
- signed_url: A URL to upload the file, returned by the server if successful.
- file_id: The ID of the newly created file in the system.
Example Usage
ws_config = actionstreamer.Config.WebServiceConfig(base_url="https://api.actionstreamer.com")
device_name = "Device1"
filename = "example_temp_file.txt"
file_size = 1024
sha256_hash = "your_sha256_hash_here"
response_code, response_string, signed_url, file_id = create_temp_file(ws_config, device_name, filename, file_size, sha256_hash)
print(response_code, response_string, signed_url, file_id)
Behavior
- Sends a
POST
request to the server to create a temporary file with the specified parameters. - If the request is successful (HTTP 200), the response will contain the signed URL and file ID.
- Returns a tuple with the response code, response message, signed URL, and file ID.
Error Handling
- General Exception: If any exception occurs during the request or processing, the function logs the error and returns an appropriate error message along with default values for the URL and file ID.